﻿using AutoMapper;
using MediatR;
using TaskProject.Application.Command.Production;
using TaskProject.Domain.BOM;
using TaskProject.Domain.Production;
using TaskProject.ErrorCode;
using TaskProject.InfrasIntructure.Implement;
using TaskProject.InfrasIntructure.Interface;

namespace TaskProject.Application.Handler.Production
{
    public class AddWorkOrderHandler : IRequestHandler<AddWorkOrderCommand, APIResult<int>>
    {
        DatabaseContext db;
        IMapper mapper;

        public AddWorkOrderHandler(DatabaseContext db, IMapper mapper)
        {
            this.db = db;
            this.mapper = mapper;
        }

        public async Task<APIResult<int>> Handle(AddWorkOrderCommand request, CancellationToken cancellationToken)
        {
            APIResult<int> res = new APIResult<int>();
            var model = mapper.Map<WorkorderModel>(request);
            int r = db.sqlSugar.Insertable(model).ExecuteCommand();
            if (r > 0)
            {
                res.Msg = "添加生产工单成功";
                res.Data = r;
                res.Code = APIEnum.Success;
               
            }
            else
            {
                res.Msg = "添加生产工单失败";
                res.Data = r;
                res.Code = APIEnum.Fail;
            }
            return await Task.FromResult(res);

        }
    }
}
